const ws = require('ws');
const server = new ws.Server({
    port: 8886
});

function init() {
    addEvent();
}

function addEvent() {
    server.on('close', close);
    server.on('error', error);
    server.on('open', open);
    server.on('connection', connection);
}

function close() {
    console.log('server close');
}

function error() {
    console.log('server error');
}

function open() {
    console.log('server open');
}

function connection(ws) {
    ws.on('message', message)
    console.log('server connection');
}

function message(data, isBinary) {
    server.clients.forEach((client) => {
        // if (client.readyState === server.OPEN) {
        //     client.send(data);
        // }
        client.send(data, {binary: isBinary});
    })
    console.log('server message', isBinary, JSON.stringify(data.toString()));
}

init();

// console.log('ws', ws);